package com.party.course.controller;


import java.util.Arrays;
import java.util.List;
import java.io.IOException;
import javax.servlet.http.HttpServletResponse;

import com.party.common.annotation.Log;
import com.party.common.config.RuoYiConfig;
import com.party.common.core.controller.BaseController;
import com.party.common.core.domain.AjaxResult;
import com.party.common.enums.BusinessType;
import com.party.common.utils.SecurityUtils;
import com.party.common.utils.StringUtils;
import com.party.common.utils.poi.ExcelUtil;
import com.party.course.util.XunpayUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.party.course.domain.RealNameInfo;
import com.party.course.service.IRealNameInfoService;
import com.party.common.core.page.TableDataInfo;

import static org.aspectj.weaver.tools.cache.SimpleCacheFactory.path;

/**
 * 实名认证Controller
 *
 * @author ruoyi
 * @date 2025-07-19
 */
@RestController
@RequestMapping("/course/realName")
public class RealNameInfoController extends BaseController {
    @Autowired
    private IRealNameInfoService realNameInfoService;

    /**
     * 查询实名认证列表
     */
//@RequiresPermissions("course:realName:list")
    @GetMapping("/list")
    public TableDataInfo list(RealNameInfo realNameInfo) {
        startPage();
        List<RealNameInfo> list = realNameInfoService.findList(realNameInfo);
        return getDataTable(list);
    }

    /**
     * 导出实名认证列表
     */
    //@RequiresPermissions("course:realName:export")
    @Log(title = "实名认证", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, RealNameInfo realNameInfo) {
        List<RealNameInfo> list = realNameInfoService.findList(realNameInfo);
        ExcelUtil<RealNameInfo> util = new ExcelUtil<RealNameInfo>(RealNameInfo.class);
        util.exportExcel(response, list, "实名认证数据");
    }

    /**
     * 获取实名认证详细信息
     */
    //@RequiresPermissions("course:realName:query")
    @GetMapping(value = "/view/{id}")
    public AjaxResult getInfo(@PathVariable("id") String id) {
        return AjaxResult.success(realNameInfoService.view(id));
    }

    @GetMapping(value = "/findByUserId/{id}")
    public AjaxResult findByUserId(@PathVariable("id") String id) {
        RealNameInfo byUserId = realNameInfoService.findByUserId(id);
        if(byUserId==null){
            // 创建一条
            RealNameInfo realNameInfo = new RealNameInfo();
            realNameInfo.setCreateBy(SecurityUtils.getUserIdStr());
            realNameInfo.setDelFlag("0");
            realNameInfoService.save(realNameInfo);
            return AjaxResult.success(realNameInfo);
        }
        return AjaxResult.success(byUserId);
    }

    /**
     * 新增实名认证
     */
    //@RequiresPermissions("course:realName:add")
    @Log(title = "实名认证", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    public AjaxResult add(@RequestBody RealNameInfo realNameInfo) {
        return toAjax(realNameInfoService.add(realNameInfo));
    }


    @PostMapping("/upload")
    public   void  upload(@RequestBody RealNameInfo realNameInfo){
        String fileType = realNameInfo.getFileType();
        String filePath = RuoYiConfig.getProfile();
        RealNameInfo RealNameInfoquery = realNameInfoService.view(realNameInfo.getId());
        if("1".equals(fileType)){
            //
            String filepath = RealNameInfoquery.getLegalLicenceFrontUrl();
            if(StringUtils.isEmpty(filepath)){
               throw new RuntimeException("请上传图片,保存后再调用");
            }
            String avatar = filepath.replace("profile/", "");
            String realPath = filePath + avatar;
             String s = XunpayUtil.uploadImage(realPath);
            RealNameInfoquery.setNetFrontUrl(s);
            System.out.println("返回的方法");
        }else if("2".equals(fileType)){
            String filepath = RealNameInfoquery.getLegalLicenceBackUrl();
            if(StringUtils.isEmpty(filepath)){
                throw new RuntimeException("请上传图片,保存后再调用");
            }
            String avatar = filepath.replace("profile/", "");
            String realPath = filePath + avatar;
             String s = XunpayUtil.uploadImage(realPath);
            RealNameInfoquery.setNetBackUrl(s);
            System.out.println("返回的方法");
        }else if("3".equals(fileType)){
            String filepath = RealNameInfoquery.getLicenceUrl();
            if(StringUtils.isEmpty(filepath)){
                throw new RuntimeException("请上传图片,保存后再调用");
            }
            String avatar = filepath.replace("profile/", "");
            String realPath = filePath + avatar;
            String s = XunpayUtil.uploadImage(realPath);
            RealNameInfoquery.setNetLicenceUrl(s);
        }else if("4".equals(fileType)){
            String filepath = RealNameInfoquery.getOpenAccountLicenceUrl();
            if(StringUtils.isEmpty(filepath)){
                throw new RuntimeException("请上传图片,保存后再调用");
            }
            String avatar = filepath.replace("profile/", "");
            String realPath = filePath + avatar;
            String s = XunpayUtil.uploadImage(realPath);
            RealNameInfoquery.setNetOpenAccountLicenceUrl(s);
        }
        realNameInfoService.updateEntity(RealNameInfoquery);
    }

    /**
     * 修改实名认证
     */
    //@RequiresPermissions("course:realName:edit")
    @Log(title = "实名认证", businessType = BusinessType.UPDATE)
    @PutMapping("/update")
    public AjaxResult edit(@RequestBody RealNameInfo realNameInfo) {
        RealNameInfo byId = realNameInfoService.getById(realNameInfo.getId());
        String legalLicenceFrontUrl = byId.getLegalLicenceFrontUrl();
        String legalLicenceBackUrl = byId.getLegalLicenceBackUrl();
        if(StringUtils.isNotEmpty(legalLicenceFrontUrl)  &&  StringUtils.isNotEmpty(realNameInfo.getLegalLicenceFrontUrl()) ){
            if(!realNameInfo.getLegalLicenceFrontUrl().equals(legalLicenceFrontUrl)){
                realNameInfo.setNetFrontUrl("");
            }
        }
        if(StringUtils.isNotEmpty(legalLicenceBackUrl)  &&  StringUtils.isNotEmpty(realNameInfo.getLegalLicenceBackUrl()) ){
            if(!realNameInfo.getLegalLicenceBackUrl().equals(legalLicenceBackUrl)){
                realNameInfo.setNetBackUrl("");
            }
        }


        return toAjax(realNameInfoService.updateEntity(realNameInfo));
    }

    /**
     * 删除实名认证
     */
    //@RequiresPermissions("course:realName:remove")
    @Log(title = "实名认证", businessType = BusinessType.DELETE)
    @DeleteMapping("/delete/{ids}")
    public AjaxResult remove(@PathVariable String[] ids) {
        return toAjax(realNameInfoService.deleteIds(Arrays.asList(ids)));
    }
}
